package qlsl.androiddesign.util.runfeng;

import java.util.ArrayList;
import java.util.List;

import qlsl.androiddesign.activity.baseactivity.BaseActivity;
import qlsl.androiddesign.adapter.subadapter.ProvinceGridAdapter;
import qlsl.androiddesign.adapter.subadapter.RepairPostAdapter;
import qlsl.androiddesign.application.SoftwareApplication;
import qlsl.androiddesign.popupwindow.subwindow.ServeTypePopupWindow;

import com.qlsl.androiddesign.kuaiwa.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

public class SelectServerTypeDialog extends Dialog implements
		View.OnClickListener {

	private ListView listView;

	private BaseActivity activity;

	public interface OnQuickOptionformClick {
		void onQuickOptionClick(int id);
	}

	private OnQuickOptionformClick mListener;

	private SelectServerTypeDialog(Context context, boolean flag,
			OnCancelListener listener) {
		super(context, flag, listener);
	}

	@SuppressLint("InflateParams")
	private SelectServerTypeDialog(Context context, int defStyle) {
		super(context, defStyle);
		View contentView = getLayoutInflater().inflate(
				R.layout.dialog_choose_server_type, null);
		// listView
		listView = (ListView) contentView.findViewById(R.id.listView);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// contentView.setOnTouchListener(new View.OnTouchListener() {
		// @Override
		// public boolean onTouch(View v, MotionEvent event) {
		// SelectServerTypeDialog.this.dismiss();
		// return true;
		// }
		// });
		super.setContentView(contentView);

	}

	public SelectServerTypeDialog(BaseActivity activity) {
		this(activity, R.style.quick_option_dialog);
		this.activity = activity;
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		getWindow().setGravity(Gravity.CENTER);

		WindowManager m = getWindow().getWindowManager();
		Display d = m.getDefaultDisplay();
		WindowManager.LayoutParams p = getWindow().getAttributes();
		p.width = d.getWidth();

		getWindow().setAttributes(p);
		initData();
	}

	private void initData() {
		String[] provinces = SoftwareApplication.getInstance().getResources()
				.getStringArray(R.array.listview_server_type);

		List<String> list = new ArrayList<String>();
		for (String text : provinces) {
			list.add(text);
		}
		RepairPostAdapter adapter = new RepairPostAdapter(activity, list);
		listView.setAdapter(adapter);
	}

	public void setOnQuickOptionformClickListener(OnQuickOptionformClick lis) {
		mListener = lis;
	}

	@Override
	public void onClick(View v) {
		final int id = v.getId();
		switch (id) {
		// case R.id.iv_close:
		// dismiss();
		// break;
		// case R.id.ly_quick_option_text:
		// onClickTweetPub(R.id.ly_quick_option_text);
		// break;
		// case R.id.ly_quick_option_album:
		// onClickTweetPub(R.id.ly_quick_option_album);
		// break;
		// case R.id.ly_quick_option_photo:
		// onClickTweetPub(R.id.ly_quick_option_photo);
		// break;
		// case R.id.ly_quick_option_voice:
		// UIHelper.showSimpleBack(getContext(), SimpleBackPage.RECORD);
		// break;
		// case R.id.ly_quick_option_scan:
		// UIHelper.showScanActivity(getContext());
		// break;
		// case R.id.ly_quick_option_note:
		// UIHelper.showSimpleBack(getContext(), SimpleBackPage.FIND_USER);
		// onClickNote();
		// UIHelper.showSimpleBack(getContext(), SimpleBackPage.FIND_USER);
		// onClickNote();
		// break;
		default:
			break;
		}
		if (mListener != null) {
			mListener.onQuickOptionClick(id);
		}
		dismiss();
	}

	// private void onClickTweetPub(int id) {
	// Bundle bundle = new Bundle();
	// int type = -1;
	// switch (id) {
	// case R.id.ly_quick_option_album:
	// type = TweetPubFragment.ACTION_TYPE_ALBUM;
	// break;
	// case R.id.ly_quick_option_photo:
	// type = TweetPubFragment.ACTION_TYPE_PHOTO;
	// break;
	// default:
	// break;
	// }
	// bundle.putInt(TweetPubFragment.ACTION_TYPE, type);
	// UIHelper.showTweetActivity(getContext(), SimpleBackPage.TWEET_PUB,
	// bundle);
	// }

	// private void onClickNote() {
	// Bundle bundle = new Bundle();
	// bundle.putInt(NoteEditFragment.NOTE_FROMWHERE_KEY,
	// NoteEditFragment.QUICK_DIALOG);
	// UIHelper.showSimpleBack(getContext(), SimpleBackPage.NOTE_EDIT, bundle);
	// }

	public String getSelectItem(View view) {
		int position = listView.getPositionForView(view);
		RepairPostAdapter adapter = (RepairPostAdapter) listView.getAdapter();
		return adapter.getItem(position);
	}
}
